package com.cashbook.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cashbook.mapper.TbCompanyMapper;
import com.cashbook.pojo.TbCompany;
import com.cashbook.pojo.TbCompanyExample;
import com.cashbook.pojo.TbCompanyExample.Criteria;
import com.cashbook.pojo.TbUser;
import com.cashbook.pojo.TbUserExample;
import com.cashbook.service.CompanyService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import entity.PageResult;
@Service
public class CompanyServiceImpl implements CompanyService {
	@Autowired
	private TbCompanyMapper companyMapper;
	/**
	 * 查询全部
	 */
	@Override
	public List<TbCompany> findAll() {
		return companyMapper.selectByExample(null);
	}
	/**
	 * 分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbCompany> page=   (Page<TbCompany>) companyMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 添加
	 */
	@Override
	public void add(TbCompany company) {
		companyMapper.insert(company);
		
	}
	/**
	 * 修改
	 */
	@Override
	public void update(TbCompany company) {
		companyMapper.updateByPrimaryKey(company);
		
	}
	/**
	 * 通过主键查询
	 */
	@Override
	public TbCompany findOne(Long id) {
		return companyMapper.selectByPrimaryKey(id);
	}
	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids) {
			companyMapper.deleteByPrimaryKey(id);
		}
		
		
	}

	@Override
	public PageResult findPage(TbCompany company, int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
		
		TbCompanyExample example=new TbCompanyExample();
		Criteria criteria = example.createCriteria();
		if(company!=null) {
			if(company.getAddr()!=null && company.getAddr().length()>0) {
				criteria.andAddrLike("%"+company.getAddr()+"%");
			}
			if(company.getLogo()!=null && company.getLogo().length()>0) {
				criteria.andLogoLike("%"+company.getLogo()+"%");
			}
			if(company.getName()!=null && company.getName().length()>0 ) {
				criteria.andNameLike("%"+company.getName()+"%");
			}
			if(company.getPhone()!=null && company.getPhone().length()>0) {
				criteria.andPhoneLike("%"+company.getPhone()+"%");
			}
		}
		Page<TbCompany> page= (Page<TbCompany>)companyMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}
}
